package com.ipartek.formacion.migracion9.ddbb;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.ipartek.formacion.migracion9.dao.MySqlPersonaDAO;
import com.ipartek.formacion.migracion9.interfaces.InterfacePersonaDAO;

/**
 * 
 * @author Fila3+1
 */
public class MySqlDAOFactory extends DAOFactory {

	private static final String DRIVER_MYSQL = "com.mysql.jdbc.Driver";
	private static String DB_NAME;
	private static String DB_PORT;
	private static String DB_USER;
	private static String DB_PASS;
	private static String DB_SERVER;
	private static String DB_URL;

	private static final Logger log = Logger.getLogger(MySqlDAOFactory.class);

	// Constructor
	// ************************************************************************
	public MySqlDAOFactory() {
		PropertyConfigurator.configure("config/log4j.properties");
		Properties prop = new Properties();
		String rutaConfig = "config/configuracion.properties";
		try {
			FileInputStream fis = new FileInputStream(rutaConfig);
			prop.load(fis);

			DB_NAME = prop.getProperty("database");
			DB_PORT = prop.getProperty("dbport");
			DB_USER = prop.getProperty("dbuser");
			DB_PASS = prop.getProperty("dbpass");
			DB_SERVER = prop.getProperty("dbserver");
			DB_URL = "jdbc:mysql://" + DB_SERVER + ":" + DB_PORT + "/"
					+ DB_NAME;

		} catch (FileNotFoundException e) {
			log.error("FileNotFoundException " + e.getMessage());
		} catch (IOException e) {
			log.error("IOException " + e.getMessage());
		} catch (Exception e) {
			log.error("Exception " + e.getMessage());
		}
	}

	// Metodos
	// ************************************************************************

	/**
	 * Funcion que realiza la conexion a la base de datos.
	 * 
	 * @return Devuelve el objeto conexion
	 */
	public static Connection conectar() {
		Connection conexion = null; // Creamos un tipo connection
		try {
			Class.forName(DRIVER_MYSQL);

			// Define URL of database server for
			// database named mysql on the localhost
			// with the default port number 3306.
			// Get a connection to the database for a
			// user named root with a blank password.
			// This user is the default administrator
			// having full privileges to do anything.

			log.trace("Conectamos con la ddbb");

			conexion = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);

			log.trace("Conectados con la ddbb " + DB_URL);
		} catch (ClassNotFoundException e) {
			log.error("ClassNotFoundException driver no encontrado "
					+ DRIVER_MYSQL);
		} catch (SQLException e) {
			while (e != null) {
				log.error("Message: " + e.getMessage() + "SQLState: "
						+ e.getSQLState() + "ErrorCode: " + e.getErrorCode());
				e = e.getNextException();
			}
		} catch (Exception e) {
			log.error("Exception general al conectar con la ddbb "
					+ e.getMessage());
		}
		log.trace("Devolvemos la conexion con la ddbb");
		return conexion;
	}

	/**
	 * Funcion que cierra la conexion
	 * 
	 * @param conexion
	 *            Conexion a cerrar
	 */
	public static void desconectar(Connection conexion) {
		if (conexion != null) {
			try {
				conexion.close();
				log.trace("Realizamos la desconexion de la ddbb");
			} catch (SQLException ex) {
				log.error("Exception general al desconectar con la ddbb "
						+ ex.getMessage());
			}
		}
	}

	/**
	 * Metodos que tengo que implementar al extender de DAOFActory
	 */
	@Override
	public InterfacePersonaDAO getInterfacePersonaDAO() {
		return new MySqlPersonaDAO();
	}

}
